# -*- coding: utf-8 -*-

import requests

def download_image_improved():
    # 伪造headers信息
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36'}
    
    # 限定url
    url = "http://news.xinhuanet.com/sports/2015-04/10/127672866_14285560280061n.jpg"
    
    # 流(stream)的方式打开
    response = requests.get(url, headers=headers, stream=True)

    from contextlib import closing
    # 关闭流（stream）
    with closing(requests.get(url, stream=True)) as response:
        # 打开文件
        with open('demo1.jpg', 'wb') as fd:
            # 每128写入一次
            for chunk in response.iter_content(128):
                fd.write(chunk)

    print response.status_code
    print response.headers

download_image_improved()
